package com.lanju.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.lanju.dao.CertificateDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.Certificate;
import com.lanju.entity.User;
import com.lanju.entity.UserIdCard;
import com.lanju.service.CertificateService;
import com.lanju.util.ObjAndMapUtil;

@Service
public class CertificateServiceImpl implements CertificateService{
	
	@Autowired
	CertificateDao certificateDao;
    @Autowired
    UserDao userDao;
	
	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> saveCertificate(Map<String,Object> map) {
		Map<String, Object> result=new HashMap<String, Object>();
		try {
			Certificate obj=new Certificate();
			obj.setCertificate_id(UUID.randomUUID().toString());
			obj.setCreate_time(new Date());
			obj.setType(map.get("type").toString());
			obj.setUrl(map.get("url").toString());
			obj.setUserId(map.get("userId").toString());
			obj.setHouseId(map.get("houseId").toString());
			certificateDao.saveCertificate(obj);
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getAllCertificateList(Map<String, Object> map) {
		Map<String,Object> result=new HashMap<>();
		try {
			List<Certificate> list=new ArrayList<Certificate>();
			list=certificateDao.getAllCertificateList(map.get("houseId").toString(),"'houseCard','bankCard'");
			List<Certificate> otherlist=certificateDao.getAllCertificateList(map.get("houseId").toString(),"'othercard'");
			if(otherlist != null && otherlist.size()>0){
				Certificate c= new Certificate();
				for(int i=0;i<otherlist.size();i++){
					Certificate model=otherlist.get(i);
					c.setCertificate_id(model.getCertificate_id());
					c.setCreate_time(model.getCreate_time());
					c.setCreator(model.getCreator());
					c.setHouseId(model.getHouseId());
					c.setType(model.getType());
					c.setUrl((c.getUrl()==null?"":c.getUrl())+model.getUrl()+",");
				}
				list.add(c);
			}
			result.put("list", list);
			result.put("success", true);
			result.put("msg", "获取成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "获取失败");
		}
		return result;
	}
	
	@Override
	public Certificate getCertificate(Map<String,Object> map){
		return certificateDao.getCertificate(map.get("houseId").toString(), map.get("type").toString());
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public int delCertificate(Map<String, Object> map) {
		return certificateDao.delCertificate(map);
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateCertificate(Map<String, Object> map) {
		Map<String, Object> result=new HashMap<String, Object>();
		try {
			Certificate obj=(Certificate) ObjAndMapUtil.mapToObject(map, Certificate.class);
			obj.setType(map.get("type").toString());
			obj.setUrl(map.get("url").toString());
			obj.setUserId(map.get("userId").toString());
			obj.setHouseId(map.get("houseId").toString());
			certificateDao.updateCertificate(obj);
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getUserIdCardInfo(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			User u=userDao.getById(map.get("userId").toString());
			result.put("imglist", u.getIdCardset());
			result.put("userName", u.getRealName());
			result.put("job", u.getJobName());
			result.put("sex", u.getSex()==0?"男":"女");
			result.put("idcardNo", u.getIdCard());
			result.put("phone", u.getPhone());
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> addIdCard(Map<String, Object> param) {
		Map<String, Object> result=new HashMap<>(); 
		try {

			User user=userDao.getById(param.get("userId").toString());
			if(user.getIdCardset() !=null && user.getIdCardset().size() >0){
				if(!StringUtils.isEmpty(param.get("delArr"))){
					JSONArray arr=	JSONArray.parseArray(param.get("delArr").toString());
					if(arr !=null && arr.size()>0){
						String urls="";
						for(int i=0;i<arr.size();i++){
							urls+="'"+arr.get(i)+"',";
						}
						System.out.println("urls=="+urls);
						param.put("imgUrl",urls.substring(0, urls.length()-1));
						certificateDao.delCard(param);
					}
				}
			}
			if(!StringUtils.isEmpty(param.get("zhengmianurl"))){
				UserIdCard idcard=new UserIdCard();
				idcard.setImgId(UUID.randomUUID().toString());
				idcard.setImgUrl(param.get("zhengmianurl").toString());
				idcard.setUser(user);
				certificateDao.addIdCard(idcard);
			}
			if(!StringUtils.isEmpty(param.get("fanmianurl"))){
				UserIdCard idcard1=new UserIdCard();
				idcard1.setImgId(UUID.randomUUID().toString());
				idcard1.setImgUrl(param.get("fanmianurl").toString());
				idcard1.setUser(user);
				certificateDao.addIdCard(idcard1);
			}
			
			result.put("success", true);
 		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		return result;
	}


}
